package com.udp2;

import java.util.ArrayList;
import java.util.List;

import com.udp.entity.BsmEntity;
import com.udp.entity.MapEntity;
import com.udp.entity.PositionOffset;
import com.udp.entity.SpatEntity;
import com.udp.entity.TimEntity;


/**
 * Created by sh04799 on 2017/10/19.
 * 16进制数据解析
 */

public class DataHandler {
    private DataHandler() {

    }


    @Deprecated
    private static BsmEntity getBsm(String data) {
        if (data==null || data.length()==0) {
            data = ""
                    + "304f8001"
                    + "02812661"
                    + "74e41fab"
                    + "c418"
                    + "1280769c"// 28
                    + "48448ca2"// 36
                    + "00c8"// 44
                    + "78184000"
                    + "e001"// 56
                    + "7080"
                    + "7ffff907"
                    + "d1817fff"
                    + "0000"
                    + "2d0"//84-87
                    + "14a"//87-90
                    + "a20aa20880027fff810200c8a316b114800100a106800100810100820100830100840100";
        }
        String lat = data.substring(28, 28 + 8);
        String lon = data.substring(36, 36 + 8);
        String altitude = data.substring(44, 44 + 4);
        String speed = data.substring(56, 56 + 4);
        String carWidth = data.substring(84, 84 + 3);
        String carLong = data.substring(87, 87 + 3);

        BsmEntity entity =
                new BsmEntity(Long.parseLong(lat, 16), Long.parseLong(lon, 16), Integer.parseInt(altitude, 16), Integer.parseInt(speed, 16), Integer.parseInt(carWidth, 16),
                        Integer.parseInt(carLong, 16));
        return entity;

    }

    @Deprecated
    private static ArrayList<SpatEntity> getSpat(String data) {
        if (data==null || data.length()==0) {
            data = ""
                    +
                    "306180010da25c305a810400000000820100a54f30108202"
                    + "0102"// 48 52
                    + "8301"
                    + "02"//56 58
                    + "8601"
                    + "1d"//62 64
                    + "8701"
                    + "03"//68 70
                    + "8b01"
                    + "03"//74 76
                    + "30128204"
                    + "03040506"//////84 92
                    + "8301"
                    + "04"//96 98
                    + "8601"
                    + "6c"//102 104
                    + "8701"
                    + "03"//108 110
                    + "8b01"
                    + "03"//114 116
                    + "30118203"
                    + "070809"//124 130
                    + "8301"
                    + "02"//134 136
                    + "8601"
                    + "1d"//140 142
                    + "8701"
                    + "03"//146 148
                    + "8b01"
                    + "03"//152 154
                    + "30148205"
                    + "0a0b0c0d0e"//162 172
                    + "8301"
                    + "02"//176 178
                    + "8602"
                    + "0198"//182 186
                    + "8701"
                    + "03"// 190 192
                    + "8b01"
                    + "03";//194 196
        }

        String laneSet = data.substring(48, 52);
        String currState = data.substring(56, 58);
        String timeToChange = data.substring(62, 64);
        String timeLikeklyToChange = data.substring(68, 70);

        String laneSet1 = data.substring(84, 92);
        String currState1 = data.substring(96, 98);
        String timeToChange1 = data.substring(102, 104);
        String timeLikeklyToChange1 = data.substring(108, 110);

        String laneSet2 = data.substring(124, 130);
        String currState2 = data.substring(134, 136);
        String timeToChange2 = data.substring(140, 142);
        String timeLikeklyToChange2 = data.substring(146, 148);

        String laneSet3 = data.substring(162, 172);
        String currState3 = data.substring(176, 178);
        String timeToChange3 = data.substring(182, 186);
        String timeLikeklyToChange3 = data.substring(190, 192);

        ArrayList<SpatEntity> list = new ArrayList<SpatEntity>();
        SpatEntity entity =
                new SpatEntity(0, laneSet, Integer.parseInt(currState, 16), Integer.parseInt(timeToChange, 16));

        SpatEntity entity1 =
                new SpatEntity(0, laneSet1, Integer.parseInt(currState1, 16), Integer.parseInt(timeToChange1, 16));

        SpatEntity entity2 =
                new SpatEntity(0, laneSet2, Integer.parseInt(currState2, 16), Integer.parseInt(timeToChange2, 16));

        SpatEntity entity3 =
                new SpatEntity(0, laneSet3, Integer.parseInt(currState3, 16), Integer.parseInt(timeToChange3, 16));
        list.add(entity);
        list.add(entity1);
        list.add(entity2);
        list.add(entity3);
        return list;

    }

    /**
     * 简化版
     *
     * @param data
     * @return
     */
    @Deprecated
    private static List<SpatEntity> getSpat11111111(String data) {
        if (data==null || data.length()==0) {
            data = "305780010da2523050810400000000820100a545300f8201"
                    + "01"//(1)laneSet 48  48+2
                    + "8301"
                    + "02"//(2)currState 54 54+2
                    + "8601"
                    + "1d"//(3) 60 60+2
                    + "8701"
                    + "03"//(4) 66 66+2
                    + "8b0103300f8201"
                    + "02"//(1)//82 82+2
                    + "8301"
                    + "04"//(2)currState 88 88+2
                    + "8601"
                    + "6c"//(3)timeToChange 94 94+2
                    + "8701"
                    + "03"//(4) 100 100+2
                    + "8b01"
                    + "03300f8201"
                    + "03"//(1) 116 116+2
                    + "8301"
                    + "02"//(2)currState 122 122+2
                    + "8601"
                    + "1d"//(3)timeToChange 128 2
                    + "8701"
                    + "03"//(4) 134 2
                    + "8b010330108201"
                    + "04"//(1) 150 2
                    + "8301"
                    + "02"//(2)currState 156 2
                    + "8602"
                    + "0198"//(3)  162 4
                    + "8701"
                    + "03"//(4) 170 170+2
                    + "8b01"
                    + "03";//(4) 176 2
        }

        String laneSet = data.substring(48, 50);
        String currState = data.substring(54, 56);
        String timeToChange = data.substring(60, 62);
        String timeLikeklyToChange = data.substring(66, 68);

        String laneSet1 = data.substring(82, 84);
        String currState1 = data.substring(88, 90);
        String timeToChange1 = data.substring(94, 96);
        String timeLikeklyToChange1 = data.substring(100, 102);

        String laneSet2 = data.substring(116, 118);
        String currState2 = data.substring(122, 124);
        String timeToChange2 = data.substring(128, 130);
        String timeLikeklyToChange2 = data.substring(134, 136);

        String laneSet3 = data.substring(150, 152);
        String currState3 = data.substring(156, 158);
        String timeToChange3 = data.substring(162, 166);
        String timeLikeklyToChange3 = data.substring(170, 172);

        List<SpatEntity> list = new ArrayList<SpatEntity>();
        SpatEntity entity =
                new SpatEntity(0, laneSet, Integer.parseInt(currState, 16), Integer.parseInt(timeToChange, 16));

        SpatEntity entity1 =
                new SpatEntity(0, laneSet1, Integer.parseInt(currState1, 16), Integer.parseInt(timeToChange1, 16));

        SpatEntity entity2 =
                new SpatEntity(0, laneSet2, Integer.parseInt(currState2, 16), Integer.parseInt(timeToChange2, 16));

        SpatEntity entity3 =
                new SpatEntity(0, laneSet3, Integer.parseInt(currState3, 16), Integer.parseInt(timeToChange3, 16));
        list.add(entity);
        list.add(entity1);
        list.add(entity2);
        list.add(entity3);
        return list;

    }

    public static TimEntity getTim(String data) {
        if (data==null || data.length()==0) {
            data = ""
                    + "30818e80"
                    + "01"
                    + "10" //msgid 10 12
                    + "8109"
                    + "00000000005f494345" //packageid 16 16+18
                    + "830101"
                    + "a4773075"
                    + "8001"
                    + "02"//52 54
                    + "a1"
                    + "1ba119a0"
                    + "108004"
                    + "1298b13a"//70 78
                    + "8104"
                    + "4863674c"// 82 90
                    + "8202"
                    + "00a5"//94 98
                    + "8102"
                    + "000f"// 102 106
                    + "8201"
                    + "02"//110 112
                    + "8202"
                    + "07e0"//116 120
                    + "8303"
                    + "05aeb0"// 124 130
                    + "8402"
                    + "7d00"// 134 138
                    + "8501"
                    + "02"//142 144
                    + "a6108004"
                    + "1298b13a"// 152 160
                    + "8104"
                    + "4863674c"//164 172
                    + "8202"
                    + "00a5"//176 180
                    + "8702"
                    + "0258"//184 188
                    + "8801"
                    + "00"//192 194
                    + "a91e301c8002"
                    + "000f"// 206 210
                    + "a216a014a3120404"
                    + "14090d8e"// 226 234
                    + "0404"
                    + "17260fa1"//238 246
                    + "0404"
                    + "12b30c89"//250 258
                    + "aa0aa0083006a0048002171885020000";
        }
        String msgid = data.substring(10, 12);
        String packageid = data.substring(16, 34);
        String roadSignID = data.substring(52, 54);
        String lat = data.substring(70, 78);
        String lon = data.substring(82, 90);
        String altitude = data.substring(94, 98);
        String viewAngle = data.substring(102, 106);
        String mutcdCode = data.substring(110, 112);

        String startYear = data.substring(116, 120);
        String startTime = data.substring(124, 130);
        String durationTime = data.substring(134, 138);

        String priority = data.substring(142, 144);//优先级

        String commonAnchor_lat = data.substring(152, 160);//起点
        String commonAnchor_lon = data.substring(164, 172);
        String commonAnchor_elevation = data.substring(176, 180);
        String commonLaneWidth = data.substring(184, 188);//车道宽度
        String commonDirectionality = data.substring(192, 194);
        List<PositionOffset> nodeList = new ArrayList<PositionOffset>();//

//        String x = data.substring(226, 230);
//        String y = data.substring(230, 234);
//
//        String x1 = data.substring(238, 242);
//        String y1 = data.substring(242, 246);
//
//        String x2 = data.substring(250, 254);
//        String y2 = data.substring(254, 258);
//
//        PositionOffset offset = new PositionOffset(Integer.parseInt(x, 16), Integer.parseInt(y, 16));
//        PositionOffset offset1 = new PositionOffset(Integer.parseInt(x1, 16), Integer.parseInt(y1, 16));
//        PositionOffset offset2 = new PositionOffset(Integer.parseInt(x2, 16), Integer.parseInt(y2, 16));

        //有符号数
        String x = data.substring(226, 230);
        String y = data.substring(230, 234);

        String x1 = data.substring(238, 242);
        String y1 = data.substring(242, 246);

        String x2 = data.substring(250, 254);
        String y2 = data.substring(254, 258);

        PositionOffset offset = new PositionOffset(getValue(x), getValue(y));
        PositionOffset offset1 = new PositionOffset(getValue(x1), getValue(y1));
        PositionOffset offset2 = new PositionOffset(getValue(x2), getValue(y2));

        nodeList.add(offset);
        nodeList.add(offset1);
        nodeList.add(offset2);
        TimEntity entity =
                new TimEntity(Integer.parseInt(msgid, 16), packageid, Integer.parseInt(roadSignID, 16), Integer.parseInt(lat, 16), Integer.parseInt(lon, 16), Integer.parseInt(altitude, 16), Integer.parseInt(viewAngle, 16),
                        Integer.parseInt(mutcdCode, 16),
                        Integer.parseInt(startYear, 16), Integer.parseInt(startTime, 16), Integer.parseInt(durationTime, 16), Integer.parseInt(priority, 16),
                        Integer.parseInt(commonAnchor_lat, 16), Integer.parseInt(commonAnchor_lon, 16), Integer.parseInt(commonAnchor_elevation, 16),
                        Integer.parseInt(commonLaneWidth, 16),
                        Integer.parseInt(commonDirectionality, 16), nodeList);
        return entity;

    }

    public static ArrayList<MapEntity> getMap(String data) {// 558
        if (data==null || data.length()==0) {
            data = "" + "30820113800107810100830103A58201023081FF8102" + "007B"// id0
                    // 44
                    // 48
                    + "A20C8004" + "191B0080"// 56 64
                    + "8104" + "CE74E500"// 68 76
                    + "A781EA3039A237800A" + "4E6F727468626F756E64"// 94 114
                    + "8101" + "01"// id1 118 120
                    + "A22630248001" + "01"// 132 34
                    + "8102" + "016E"// 138 142
                    + "8201" + "02"// 146 148
                    + "A318" + ""
                    + "0406" + "E1B1CBF70001"// nodelist 156 168
                    + "0406" + "62139F380002"// 172 184
                    + "0406" + "AF339F380001"// 188 200
                    + "3038A2368009" + "45617374626F756E64"// 212 230
                    + "8101" + "02"// id2 234 236
                    + "A22630248001" + "02"// 248 250
                    + "8102" + "016E"// 254 258
                    + "8201" + "02"// 262 264
                    + "A318" + ""
                    + "0406" + "D10459060001" //nodelist 272 284
                    + "0406" + "50DA9F380001"// 288 300
                    + "0406" + "50EC9F380000"// 304 316
                    + "3039A237800A" + "536F757468626F756E64"// 328 348
                    + "8101" + "03"// id3 352 354
                    + "A22630248001" + "03"// 366 368
                    + "8102" + "016E"// 372 376
                    + "8201" + "02"// 380 382
                    + "A318"
                    + "0406" + "C32C2AC00001"//nodelist 390 402
                    + "0406" + "5E79122A0001"// 406 418
                    + "0406" + "AF33AF650001"// 422 434
                    + "3038A2368009" + "57657374626F756E64"// 446 464
                    + "8101" + "04" // id4 468 470
                    + "A22630248001" + "04"// 482 484
                    + "8102" + "016E"// 488 492
                    + "8201" + "02"// 496 498
                    + "A318"
                    + "0406" + "E038795E0001" //nodelist 506 518
                    + "0406" + "50C4122A0001"// 522 534
                    + "0406" + "9A5460C80001"// 538 550
                    + "87023500";
        }

        String id = data.substring(44, 48);
        String lat = data.substring(56, 64);
        String lon = data.substring(68, 76);
        MapEntity entity = new MapEntity(Long.parseLong(lat, 16),
                Long.parseLong(lon, 16), Integer.parseInt(id, 16));
        // id1
        String name1 = data.substring(94, 114);
        String id1 = data.substring(118, 120);
        String laneNumber1 = data.substring(132, 134);
        String laneWidth1 = data.substring(138, 142);
        String laneAttributes1 = data.substring(146, 148);
        ArrayList<PositionOffset> list1 = new ArrayList<PositionOffset>();
        String x11 = data.substring(156, 160);
        String y11 = data.substring(160, 164);
        String z11 = data.substring(164, 168);

        PositionOffset offset11 = new PositionOffset(getValue(x11),
                getValue(y11), getValue(z11));


        String x12 = data.substring(172, 176);
        String y12 = data.substring(176, 180);
        String z12 = data.substring(180, 184);
        PositionOffset offset12 = new PositionOffset(getValue(x12),
                getValue(y12), getValue(z12));

        String x13 = data.substring(188, 192);
        String y13 = data.substring(192, 196);
        String z13 = data.substring(196, 200);
        PositionOffset offset13 = new PositionOffset(getValue(x13),
                getValue(y13), getValue(z13));
        list1.add(offset11);
        list1.add(offset12);
        list1.add(offset13);

        MapEntity entity1 = new MapEntity(Long.parseLong(lat, 16),
                Long.parseLong(lon, 16), Integer.parseInt(id1, 16),
                Integer.parseInt(laneNumber1, 16), Integer.parseInt(laneWidth1,
                16), Integer.parseInt(laneAttributes1, 16), list1,
                name1);
        // id2
        String name2 = data.substring(212, 230);
        String id2 = data.substring(234, 236);
        String laneNumber2 = data.substring(248, 250);
        String laneWidth2 = data.substring(254, 258);
        String laneAttributes2 = data.substring(262, 264);
        ArrayList<PositionOffset> list2 = new ArrayList<PositionOffset>();
        String x21 = data.substring(272, 276);
        String y21 = data.substring(276, 280);
        String z21 = data.substring(280, 284);
        PositionOffset offset21 = new PositionOffset(getValue(x21),
                getValue(y21), getValue(z21));

        String x22 = data.substring(288, 292);
        String y22 = data.substring(292, 296);
        String z22 = data.substring(296, 300);
        PositionOffset offset22 = new PositionOffset(getValue(x22),
                getValue(y22), getValue(z22));

        String x23 = data.substring(304, 308);
        String y23 = data.substring(308, 312);
        String z23 = data.substring(312, 316);
        PositionOffset offset23 = new PositionOffset(getValue(x23),
                getValue(y23), getValue(z23));
        list2.add(offset21);
        list2.add(offset22);
        list2.add(offset23);

        MapEntity entity2 = new MapEntity(Long.parseLong(lat, 16),
                Long.parseLong(lon, 16), Integer.parseInt(id2, 16),
                Integer.parseInt(laneNumber2, 16), Integer.parseInt(laneWidth2,
                16), Integer.parseInt(laneAttributes2, 16), list2,
                name2);

        // //id3
        String name3 = data.substring(212, 230);
        String id3 = data.substring(352, 354);
        String laneNumber3 = data.substring(366, 368);
        String laneWidth3 = data.substring(372, 376);
        String laneAttributes3 = data.substring(380, 382);
        ArrayList<PositionOffset> list3 = new ArrayList<PositionOffset>();
        String x31 = data.substring(390, 394);
        String y31 = data.substring(394, 398);
        String z31 = data.substring(398, 402);
        PositionOffset offset31 = new PositionOffset(getValue(x31),
                getValue(y31), getValue(z31));
        String x32 = data.substring(406, 410);
        String y32 = data.substring(410, 414);
        String z32 = data.substring(414, 418);
        PositionOffset offset32 = new PositionOffset(getValue(x32),
                getValue(y32), getValue(z32));

        String x33 = data.substring(422, 426);
        String y33 = data.substring(426, 430);
        String z33 = data.substring(430, 434);
        PositionOffset offset33 = new PositionOffset(getValue(x33),
                getValue(y33), getValue(z33));
        list3.add(offset31);
        list3.add(offset32);
        list3.add(offset33);

        int id33 = Integer.parseInt(id3, 16);
        int laneNumber33 = Integer.parseInt(laneNumber3, 16);
        int laneWidth33 = Integer.parseInt(laneWidth3, 16);
        int laneAttributes33 = Integer.parseInt(laneAttributes3, 16);

        MapEntity entity3 = new MapEntity(Long.parseLong(lat, 16),
                Long.parseLong(lon, 16), id33, laneNumber33, laneWidth33,
                laneAttributes33, list3, name3);

        // id4
        String name4 = data.substring(446, 464);
        String id4 = data.substring(468, 470);
        String laneNumber4 = data.substring(482, 484);
        String laneWidth4 = data.substring(488, 492);
        String laneAttributes4 = data.substring(496, 498);
        ArrayList<PositionOffset> list4 = new ArrayList<PositionOffset>();
        String x41 = data.substring(506, 510);
        String y41 = data.substring(510, 514);
        String z41 = data.substring(514, 518);

        PositionOffset offset41 = new PositionOffset(getValue(x41),
                getValue(y41), getValue(z41));
        String x42 = data.substring(522, 526);
        String y42 = data.substring(526, 530);
        String z42 = data.substring(530, 534);
        PositionOffset offset42 = new PositionOffset(getValue(x42),
                getValue(y42), getValue(z42));

        String x43 = data.substring(538, 542);
        String y43 = data.substring(542, 546);
        String z43 = data.substring(546, 550);
        PositionOffset offset43 = new PositionOffset(getValue(x43),
                getValue(y43), getValue(z43));
        list4.add(offset41);
        list4.add(offset42);
        list4.add(offset43);

        MapEntity entity4 = new MapEntity(Long.parseLong(lat, 16),
                Long.parseLong(lon, 16), Integer.parseInt(id4, 16),
                Integer.parseInt(laneNumber4, 16), Integer.parseInt(laneWidth4,
                16), Integer.parseInt(laneAttributes4, 16), list4,
                name4);

        ArrayList<MapEntity> list = new ArrayList<MapEntity>();
        list.add(entity);
        list.add(entity1);
        list.add(entity2);
        list.add(entity3);
        list.add(entity4);
        return list;
    }

    public static ArrayList<SpatEntity> getSpat2(String data) {
        if (data==null || data.length()==0) {
            data = ""
                    + "305480010DA24F304D8102"
                    + "007B"//id 22 26
                    + "820100A544300F8201"
                    + "01"//laneSet 44 46
                    + "8301"
                    + "01"// 50 52
                    + "8601"
                    + "1D"// 5658
                    + "8701038b0103300f8201"
                    + "02"//laneSet  78 80
                    + "8301"
                    + "04"//84 86
                    + "8601"
                    + "6c"//90 92
                    + "8701038b0103300f8201"
                    + "03"//laneSet 112 114
                    + "8301"
                    + "02"//118 120
                    + "8601"
                    + "1d"// 124 126
                    + "8701038b0103300F8201"
                    + "04"//laneSet  146 148
                    + "8301"
                    + "02"//152 154
                    + "8601"
                    + "1D"// 158 160
                    + "8701038b0103";
        }

        String id = data.substring(22, 26);
        String laneSet = data.substring(44, 46);
        String currState = data.substring(50, 52);
        String timeToChange = data.substring(56, 58);

        String laneSet1 = data.substring(78, 80);
        String currState1 = data.substring(84, 86);
        String timeToChange1 = data.substring(90, 92);

        String laneSet2 = data.substring(112, 114);
        String currState2 = data.substring(118, 120);
        String timeToChange2 = data.substring(124, 126);

        String laneSet3 = data.substring(146, 148);
        String currState3 = data.substring(152, 154);
        String timeToChange3 = data.substring(158, 160);

        ArrayList<SpatEntity> list = new ArrayList<SpatEntity>();
        SpatEntity entity = new SpatEntity(Integer.parseInt(id, 16), laneSet,
                Integer.parseInt(currState, 16), Integer.parseInt(timeToChange,
                16));

        SpatEntity entity1 = new SpatEntity(Integer.parseInt(id, 16), laneSet1,
                Integer.parseInt(currState1, 16), Integer.parseInt(
                timeToChange1, 16));

        SpatEntity entity2 = new SpatEntity(Integer.parseInt(id, 16), laneSet2,
                Integer.parseInt(currState2, 16), Integer.parseInt(
                timeToChange2, 16));

        SpatEntity entity3 = new SpatEntity(Integer.parseInt(id, 16), laneSet3,
                Integer.parseInt(currState3, 16), Integer.parseInt(
                timeToChange3, 16));
        list.add(entity);
        list.add(entity1);
        list.add(entity2);
        list.add(entity3);
        return list;

    }

    public static BsmEntity getBsm2(String data) {
        if (data==null || data.length()==0) {
            data = ""
                    + "6174e41fabc418"
                    + "1280769c"// 14 22
                    + "48448ca2"// 22 30
                    + "00c8"//30 34
                    + "78184000"
                    + "e001"//42 46
                    + "70807ffff907d1817fff0000"
                    + "2d014a";//70 76
        }


        String lat = data.substring(14, 14 + 8);
        String lon = data.substring(22, 22 + 8);
        String altitude = data.substring(30, 30 + 4);
//        String speed = data.substring(42, 42 + 4);
        String speed = data.substring(43, 43 + 3);// 计算速度
        String carWidth = data.substring(70, 70 + 3);
        String carLong = data.substring(73, 73 + 3);

        BsmEntity entity = new BsmEntity(Long.parseLong(lat, 16),
                Long.parseLong(lon, 16), Integer.parseInt(altitude, 16),
                Integer.parseInt(speed, 16), Integer.parseInt(carWidth, 16),
                Integer.parseInt(carLong, 16));
        return entity;
    }


    private static int getValue(String hex) {

//        已知一个负数的补码，将其转换为十进制数，步骤
//        1、先对各位取反；
//        2、将其转换为十进制数；
//        3、加上负号，再减去1。
        int value;
        String bin = Integer.toBinaryString(Integer.valueOf(hex, 16));
        int first = Integer.parseInt(hex.substring(0, 1), 16);
        if (first < 8) {
            value = Integer.parseInt(hex, 16);
        } else if (hex.startsWith("0")) {
            value = Integer.parseInt(hex, 16);
        } else if (bin.startsWith("0")) {
            value = Integer.parseInt(hex, 16);

        } else {
            char[] array = bin.toCharArray();
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i]);
                char temp = array[i];
                if (temp == '0') {
                    array[i] = '1';
                } else {
                    array[i] = '0';
                }

            }
            value = -Integer.valueOf(new String(array), 2) - 1;
        }
        return value;
    }

}
